package thread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author jiangwz
 * @create 2019/9/15.
 */
public class CallableDemo {

    /**
     * 这段代码很精髓
     * Thread类是没有传Callable的构造器的
     * 只有传Runnable构造器
     *
     * 为了使Callable生效,自己写了一个RunnableFuture接口继承Runnable
     *public interface RunnableFuture<V> extends Runnable, Future<V> {
     *     void run();
     *}
     *然后使用 FutureTask类 实现 RunnableFuture,再把FutureTask对象传入thread的构造器中,进行扩展
     * 要研究这个思想,很精髓,学会可随意扩展代码架构
     *
     *
     *
     * @param args
     */
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        MyThread myThread = new MyThread();
        FutureTask<Integer> futureTask = new FutureTask<>(new MyThread());

        Thread thread = new Thread(futureTask,"aa");
        thread.start();
        Integer integer = futureTask.get();
        System.out.println(integer);
    }


}

class MyThread implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        return 1024;
    }
}